package com.allen.control;

import com.allen.bean.Stock;
import com.allen.service.StockService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/stockCtrl")
public class StockControl extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String parm = req.getParameter("parm");
        switch (parm) {
            case "list":
                list(req, resp);
                break;
            case "modify":
                modify(req, resp);
                break;
        }
    }

    private void modify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        new StockService().updateNum(req, resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Stock> stocks = new StockService().getAll();
        req.setAttribute("stocks", stocks);
        req.getRequestDispatcher("/stock/list.jsp").forward(req, resp);
    }
}
